const Service = require('egg').Service;

class GoodsService extends Service {
  async find(params) {
    var {min,max,limit,offset} = params
    if(min==undefined && max==undefined){
      // if(limit==undefined){
      //   limit=8
      // }
      // if(offset==undefined){
      //   offset=0
      // }
      var goods = await this.ctx.model.Goods.find().skip(Number(offset)).limit(Number(limit))
      var total = await this.ctx.model.Goods.find().count()
      var msg = '分页相关数据'
    }else{
        goods = await this.ctx.model.Goods.find({salePrice:{$gt:Number(min),$lt:Number(max)}})
        total = goods.length
        msg='价格区间相关数据'
    }
    return {goods,total,msg};
  }
  async sortPrice(params){
    var {price} = params;
    var data = await this.ctx.model.Goods.find().sort({salePrice:Number(price)});
    return data;
  }
}

module.exports = GoodsService;